//author:kakake
class tsUserDao {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    /**加载机构 */
    getWorkerList() {
        let strsql = "select * from BaseWorkers";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
    }

    /**获取当前机构 */
    getCurrWorker(workId) {
        let strsql = "select * from BaseWorkers where WorkId=@workId";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { workId: workId });
    }

    /**重置用户密码 */
    resetPassword(userId, password) {
        let strsql = "update baseuser set PassWord=@password where userid=@userId";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { userId: userId, password: password });
    }

    /**查询用户 */
    searchUser(key) {
        let strsql = "select a.empid,b.userid,b.Code,a.Name,a.sex,c.DeptId,b.Lock from \
                                BaseEmployee a left join BaseUser b on a.EmpId=b.EmpId\
                                left join BaseEmpDept c on a.EmpId=c.EmpId and c.DefaultFlag=1\
                                where (b.code like @key or a.name like @key or a.pym like @key or a.wbm like @key or a.szm like @key) and a.workId=@workid";
        let dbparams = {
            key: '%' + key + '%',
            workid: this.data.WorkId
        }
        //TSLobsterAPI.Utils.TrackLog('searchUser', [strsql, dbparams]);
        return TSLobsterAPI.DbHelper.DbGetJsonTable(this.data.dbConn, strsql, dbparams);
    }

    /**查询用户通过科室id */
    searchUserByDept(deptIds) {
        let strsql = "select a.empid,c.userid,c.Code,b.Name,b.sex,a.DeptId,c.Lock from\
                                BaseEmpDept a left join BaseEmployee b on a.EmpId=b.EmpId\
                                left join BaseUser c on a.EmpId=c.EmpId\
                                where a.DeptId in ("+ deptIds + ") and a.DefaultFlag=1 and c.workId=@workid";
        return TSLobsterAPI.DbHelper.DbGetJsonTable(this.data.dbConn, strsql, { workid: this.data.WorkId });
    }

    /**获取字典 */
    getDictData(classid) {
        let strsql = "SELECT Code ,Name ,Pym ,Wbm ,Szm ,Memo FROM BaseDictContent WHERE ClassId=@classid AND DelFlag=0 ORDER BY SortOrder ASC ";
        return TSLobsterAPI.DbHelper.DbGetJsonTable(this.data.dbConn, strsql, { classid: classid });
    }


    /**获取人员默认科室 */
    getDefaultDept(empid) {
        let strsql = "select distinct b.* from BaseEmpDept a left join basedept b on a.deptid=b.deptid where a.DefaultFlag=1 AND b.DeptId IS NOT NULL and a.EmpId=@empid";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { empid: empid });
    }

    /**获取用户角色 */
    getGroupList(userId) {
        let strsql = "select b.* from BaseGroupUser a left join BaseGroup b on a.GroupId=b.GroupId \
                                where a.UserId=@userId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { userId: userId });
    }

    /**获取人员所属科室 */
    getHaveDept(empId) {
        let strsql = "select distinct b.* from BaseEmpDept a left join BaseDept b on a.DeptId=b.DeptId where a.EmpId=@empId and b.DeptId is not null";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { empId: empId });
    }

    /**删除科室分类 */
    deleteDeptLayer(layerId) {
        let strsql = "delete from BaseDeptLayer where LayerId=@layerId";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { layerId: layerId });
    }

    /**删除科室 */
    deleteDept(deptId) {
        let strsql = "update basedept set DelFlag=1 where deptId=@deptId";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { deptId: deptId });
    }

    /**判断用户名是否存在 */
    existUserCode(usercode, userid: number): boolean {
        let strsql = "select count(1) from baseuser where Code=@usercode and userid!=@userid";
        let ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { usercode: usercode, userid: userid });
        if (ret > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**设置人员所管辖科室 */
    setHaveEmpDeptRole(empId, deptIds: Array<string>) {
        let strsql = "delete from BaseEmpDept where EmpId=@empId";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { empId: empId });
        deptIds.forEach(item => {
            strsql = "insert into BaseEmpDept(EmpId,DefaultFlag,DeptId,WorkId) values(@empId,0,@deptId,@workid)";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { empId: empId, deptId: parseInt(item), workid: this.data.WorkId });
        })
    }

    /**设置人员默认科室 */
    setDefaultEmpDeptRole(empId, defaultDeptId) {
        let strsql = "delete from BaseEmpDept where EmpId=@empId and DeptId=@defaultDeptId";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { empId: empId, defaultDeptId: defaultDeptId });

        strsql = "insert into BaseEmpDept(EmpId,DefaultFlag,DeptId,WorkId) values(@empId,1,@defaultDeptId,@workid)";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { empId: empId, defaultDeptId: defaultDeptId, workid: this.data.WorkId });
    }

    /**设置用户角色 */
    setGroupUserRole(userId, groupIds: Array<string>) {
        let strsql = "delete from BaseGroupUser where UserId=@userId";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { userId: userId });
        groupIds.forEach(item => {
            strsql = "insert into BaseGroupUser(UserId,GroupId,WorkId) values(@userId,@groupId,@workid)";
            TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { userId: userId, groupId: item, workid: this.data.WorkId });
        })
    }
}